package com.ll.beautymall.ui.home.categorydetail.model;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ll.beautymall.bean.Brand;
import com.ll.beautymall.bean.BrandContent;
import com.ll.beautymall.bean.BrandData;
import com.ll.beautymall.bean.BrandList;
import com.ll.beautymall.utils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/10/24.
 */
public class CategoryDetailBiz {
    public void getBrandData(HashMap<String, String> parameters, final OnGetBrandListLisenter lisenter) {
        HttpUtils.getInstance().getBrandList(parameters, new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    try {
                        String json = response.body();
                        JSONObject obj = new JSONObject(json);
                        JSONObject data = obj.getJSONObject("data");//在此可能并没有Brand，no value for data
                        JSONArray group_list = data.getJSONArray("group_list");
                        List<BrandList> brands = new ArrayList<BrandList>();
                        for (int i = 0; i < group_list.length(); i++) {
                            JSONObject jsonObject = group_list.getJSONObject(i);
                            String cids = jsonObject.getString("cids");
                            String id = jsonObject.getString("id");
                            JSONArray items = jsonObject.getJSONArray("items");
                            Gson gson = new Gson();
                            List<Brand> brandList = gson.fromJson(items.toString(), new TypeToken<List<Brand>>() {
                            }.getType());
                            String title = jsonObject.getString("title");
                            BrandList brandlist = new BrandList(cids, id, brandList, title);
                            brands.add(brandlist);
                        }
                        String sub_title = data.getString("sub_title");
                        String title = data.getString("title");
                        BrandContent content = new BrandContent(brands, sub_title, title);
                        int publish_status = obj.getInt("publish_status");
                        String publish_time = obj.getString("publish_time");
                        BrandData brandData = new BrandData(content, publish_status, publish_time);
                        if (brandData != null) {
                            lisenter.onSucced(brandData);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                    lisenter.onFailed("网络连接较慢，请检查网络...");
            }
        });
    }

}
